package com.chtw.controller;

import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.chtw.entity.StudentInfo;
import com.chtw.entity.TeacherInfo;
import com.chtw.service.StudentService;
import com.chtw.utils.POIUtils;
import com.chtw.utils.Result;
import com.github.pagehelper.PageInfo;

@Controller
@RequestMapping("/student")
public class StudentInfoContriller {

	@Autowired
	private StudentService stuService;
	
	@RequestMapping("/getAll")
	@ResponseBody
	public Result getAll() {
		List<StudentInfo> sList = stuService.getALL();
		if(sList.size()>0) {
			return Result.seccessStu(sList);
		}else{
			return Result.fail("获取失败！");
		}
		
	}
	
	/**
	 * 删除学生信息
	 * @param stuId
	 * @param session
	 * @return
	 */
	@RequestMapping("/delete")
	@ResponseBody
	public Result delete(String stuId,HttpSession session) {
		List<StudentInfo> sList = stuService.dele(Integer.parseInt(stuId));
		PageInfo<StudentInfo> info = stuService.page(1);
		if(sList!=null) {
			//session.setAttribute("total", sList.size());
			//session.setAttribute("sList", sList);
			session.setAttribute("total", info.getTotal());
			session.setAttribute("pages", info.getPages());
			return Result.seccess();
		}
		return Result.fail("删除失败！");
	}
	

	/**
	 * 分页显示信息
	 * @param nowpage
	 * @param session
	 * @return
	 */
	@RequestMapping("/getpage")
	@ResponseBody
	public Result pageinfo(@RequestParam(defaultValue="1",required=false)String nowpage,HttpSession session,StudentInfo stu) {
		PageInfo<StudentInfo> info = stuService.page(Integer.parseInt(nowpage),stu);
		//System.out.println();
		System.out.println("stuId-"+stu.getStuId()+"-->"+"stuAcademy-"+stu.getStuAcademy()+"1-->"+"stuTeacherno-"+stu.getTeacherno()+"-->"+"EntryDate-"+stu.getEntryDate());
		List<StudentInfo> sList = info.getList();
		if(sList!=null) {
			//session.setAttribute("nowpagem", info.getPageNum());
			//session.setAttribute("nowpagem",nowpage);
			//session.setAttribute("pages", info.getPages());
			//session.setAttribute("sList", sList);
			//session.setAttribute("total", info.getTotal());
			return Result.seccess(sList,info.getPages(),(int)info.getTotal());
		}
		return Result.fail("获取失败！");
	}
	
	/**
	 * 获取院系
	 * @return
	 */
	@RequestMapping("/getAcademy")
	@ResponseBody
	public Result getAcademy() {
		List<StudentInfo> aList = stuService.getAcademy();
		if(aList.size()>0) {
			return Result.seccess(aList);
		}
		return Result.fail("获取失败！");
	}
	
	/**
	 * 获取辅导员列表
	 * @return
	 */
	@RequestMapping("/gettnoList")
	@ResponseBody
	public Result gettnoList() {
		List<TeacherInfo> tnoList = stuService.getListBytno();
		if(tnoList.size()>0) {
			return Result.seccess(tnoList);
		}
		return Result.fail("获取失败！");
	}
	
	/**
	 * 获取入学日期
	 * @return
	 */
	@RequestMapping("/getintime")
	@ResponseBody
	public Result getintime() {
		List<StudentInfo> intList = stuService.getintime();
		if(intList.size()>0) {
			return Result.seccess(intList);
		}
		return Result.fail("获取失败！");
	}
	
	@RequestMapping("/getinfo")
	@ResponseBody
	public Result getinfo(String stuId,HttpSession session) {
		if(stuId.equals("00000")) {
			stuId = (String)session.getAttribute("scurrentUser");
		}
		StudentInfo stu = stuService.getinfo(Integer.parseInt(stuId));
		if(stu!=null) {
			return Result.seccess(stu);
		}else {
			return Result.fail("获取失败！");
		}
	}
	
	/**
	 * 跟新学生信息
	 * @param stu
	 * @param stuId
	 * @return
	 */
	@RequestMapping("/updatestu")
	@ResponseBody
	public Result updatestu(StudentInfo stu,String stuId) {
		int i = stuService.updatestu(stu,stuId);
		if(i>0) {
			return Result.seccess();
		}else {
			return Result.fail("修改失败！");
		}
	}
	
	
	/**
	 * 添加学生
	 * @param stu
	 * @return
	 */
	@RequestMapping("/addstu")
	@ResponseBody
	public Result addstu(StudentInfo stu,HttpSession session) {
		int i = stuService.addstu(stu);
		List<StudentInfo> sList = stuService.getALL();
		PageInfo<StudentInfo> info = stuService.page(1);
		if(sList!=null) {
			session.setAttribute("total", sList.size());
			session.setAttribute("pages", info.getPages());
			//return Result.seccess();
		}
		if(i>0) {
			return Result.seccess();
		}else {
			return Result.fail("添加失败");
		}
	}
	
	@RequestMapping("/search")
	@ResponseBody
	public Result search(StudentInfo stu,HttpSession session) {
		List<StudentInfo> seList = stuService.search(stu);
		System.out.println(stu.getStuId()+"-->"+stu.getStuAcademy()+"-->"+stu.getTeacherno()+"-->"+stu.getEntryDate());
		if(seList!=null) {
			return Result.seccess(seList);
		}
		return Result.fail("查询失败！");
	}
	
	@RequestMapping("/outFile")
	public void outFile(StudentInfo stu,HttpServletResponse response) throws IOException {
		List<StudentInfo> seList = stuService.search(stu);
		String data = JSON.toJSONString(seList);
		//System.out.println(data);
		JSONArray body = JSON.parseArray(data);
		List<Object> json = new ArrayList<Object>();
		json.add("职务");
		json.add("学号");
		json.add("入学时间");
		json.add("姓名");
		json.add("院系");
		json.add("辅导员(工号)");
		json.add("生日");
		json.add("性别");
		
		JSONArray head = new JSONArray(json);
		HSSFWorkbook wbook = POIUtils.expExcel(head,body,"学生信息");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=UTF-8");
		response.setLocale(new java.util.Locale("zh","CN"));
		response.setContentType("application/octet-stream");
		String fileName = URLEncoder.encode("学生信息.xls","UTF-8");
		response.addHeader("Content-Disposition","attachment;fileName="+fileName);
		OutputStream os = response.getOutputStream();
		wbook.write(os);
		os.close();
	}
}
